<?php
/**
 *============================
 * author:Farmer
 * time:2018/8/18 19:24
 * blog:blog.icodef.com
 * function:嵌入页面
 *============================
 */

if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

include "config.php";

class plugin_huanl_reward {

}

class plugin_huanl_reward_forum extends plugin_huanl_reward {

    public function post_reward_extra() {
        include 'script.php';
        if ($_GET['action'] == 'newthread') {
            return get_post_script();
        } else {
            return;
        }
    }
    public function forumdisplay_postbutton_bottom(){
        include 'script.php';
        return get_list_script();
    }

    /**
     * hook,post页面
     * @return string
     */
    public function post_interception() {
        global $_G;
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            //新的
            if ($_GET['action'] == 'newthread') {
                if (!isset($_POST['credit'])) {
                    return;
                }
                //判断是否在内
                $arr = explode(',', $_G['cache']['plugin']['huanl_reward']['credit_index']);
                if (!in_array($_POST['credit'], $arr)) return;
                //设置积分类型
                $_G['setting']['creditstransextra'][2] = $_POST['credit'];
            } else if ($_GET['action'] == 'edit') {
                $this->change_credit_type($_POST['tid']);
            }
        } else if (strtolower($_SERVER['REQUEST_METHOD']) == 'get' && $_GET['action'] == 'edit') {
            //编辑帖子的时候,修改积分类型
            $this->change_credit_type($_GET['tid']);
        }
    }

    /**
     * 拦截显示
     */
    public function viewthread_interception() {
        $this->change_credit_type($_GET['tid']);
    }

    protected function change_credit_type($tid) {
        global $_G;
        $row = DB::fetch_first('select * from ' .
            DB::table('reward_credit') . ' where tid=%d',
            [$tid]);
        if ($row) {
            $_G['setting']['creditstransextra'][2] = $row['ctype'];
        }
    }

    /**
     * 最佳答案
     */
    public function misc_interception() {
        if ($_GET['action'] == 'bestanswer') {
            $this->change_credit_type($_GET['tid']);
        }
    }

    /**
     * 添加悬赏新帖子,标记一下积分的类型
     * @param $param
     */
    public function post_message($param) {
        global $_G;
        if ($param['param'][0] != 'post_newthread_succeed') return;
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            //新的
            if (isset($_GET['action']) && $_GET['action'] == 'newthread') {
                if (!isset($_POST['credit'])) {
                    return;
                }
                //判断是否在内
                $arr = explode(',', $_G['cache']['plugin']['huanl_reward']['credit_index']);
                if (!in_array($_POST['credit'], $arr)) return;
                DB::insert('reward_credit', ['tid' => $param['param'][2]['tid'], 'ctype' => $_POST['credit']]);
            }
        }
    }
}


class mobileplugin_huanl_reward_forum extends plugin_huanl_reward_forum {

}


